import type { AssessmentPrice } from '@/services/price-sheet/getBwPriceAskWords'
import { priceTable, priseTableReduce } from '@/shared/priceTable'
import { cloneDeep, isNil } from 'lodash-es'
import { add, mul, toFixed } from '@/utils/math'
import { computed, type ComputedRef } from 'vue'
import type { Coordinates, usePriceCoordinates } from './usePriceCoordinates'
import type { ComboPriceTimer, DataIndex, OnLinePeriodEnum } from '@/core/enums/name'
import type { PlatformEnum, PricePlatform } from '@/core/enums/platform'
import type { Premium } from '@/core/interfaces/premium'
import is from '@/utils/is'

export interface PriceData {
  dataIndex?: DataIndex
  columnName?: PricePlatform
  quote?: string
  timer?: ComboPriceTimer
  onLinePeriod?: OnLinePeriodEnum
  active: boolean
  assessmentPrice: number
  clientType: PlatformEnum
  days: number
  onlinePeriodDays: number
  onlinePeriodHours: number
  priceTypeId: number
}

export function usePriceTable(
  coordinatesHook: ReturnType<typeof usePriceCoordinates>,
  prices: ComputedRef<AssessmentPrice[]>,
  premiums?: ComputedRef<Premium[] | number>
) {
  const { baiduCoordinates, qihu360Coordinates } = coordinatesHook

  const baiduRatio = computed(() => {
    if (isNil(premiums)) {
      return 1
    }

    if (is.number(premiums.value)) {
      return premiums.value as number
    } else {
      return ((premiums.value as Premium[]) || []).reduce((a, b) => add(a, b.premiumRatio).toNumber(), 1)
    }
  })

  const qihu360Ratio = 0.8

  // 基于系数计算出每个格子的价格
  const baiduPrice = computed(() => {
    return prices.value.map((d) => {
      return {
        ...d,
        assessmentPrice: Number(toFixed(0, mul(baiduRatio.value, d.assessmentPrice)))
      }
    })
  })

  // 基于百度单价和360系数计算出每个格子的价格
  const qihu360Price = computed(() => {
    return baiduPrice.value.map((d) => {
      return {
        ...d,
        assessmentPrice: Number(toFixed(0, mul(qihu360Ratio, d.assessmentPrice)))
      }
    })
  })

  // 基于数据迭代出百度标王表格的数据结构
  const baiduTable = computed(() => {
    return priseTableReduce(baiduPrice.value, cloneDeep(priceTable))
  })

  // 基于数据迭代出360标王表格的数据结构
  const qihu360Table = computed(() => {
    return priseTableReduce(qihu360Price.value, cloneDeep(priceTable))
  })

  // 基于坐标获得当前所选格子的百度标王的元数据
  const currentBaiduPriceData = computed<Partial<PriceData>>(() => {
    if (baiduCoordinates.value.length) {
      return getWithBaiduCoordinates(baiduCoordinates.value)
    } else {
      return {}
    }
  })

  // 基于坐标获得当前所选格子的360标王的元数据
  const current360PriceData = computed<Partial<PriceData>>(() => {
    if (qihu360Coordinates.value.length) {
      return getWithBaiduCoordinates(qihu360Coordinates.value)
    } else {
      return {}
    }
  })

  // 基于百度所选格子的价格计算出创意配图的价格（抢鲜价）
  const creativePrice = computed(() => {
    const assessmentPrice = currentBaiduPriceData.value?.assessmentPrice ?? 0
    return toFixed(0, mul(0.2, assessmentPrice))
  })

  // 基于百度所选格子的价格计算出创意配图的价格（原价）
  const creativeOriginPrice = computed(() => {
    const assessmentPrice = currentBaiduPriceData.value?.assessmentPrice ?? 0
    return toFixed(0, mul(0.3, assessmentPrice))
  })

  const current360Price = computed(() => {
    // 用360的行索引和列索引得到baidu的对应价格
    const baiduAssessmentPrice = getWithBaiduCoordinates(qihu360Coordinates.value)?.assessmentPrice ?? 0
    // assessmentPrice存在说明 百度标王被勾选，则说明是是打包出售(百度+360)，所以3折，否则5折
    return toFixed(0, mul(currentBaiduPriceData.value.assessmentPrice ? 0.3 : 0.5, baiduAssessmentPrice))
  })

  const current360OriginPrice = computed(() => {
    return current360PriceData.value?.assessmentPrice ?? 0
  })

  const currentBaiduPrice = computed(() => {
    return currentBaiduPriceData.value.assessmentPrice
  })

  // 基于坐标获得格子的元数据
  function getWithBaiduCoordinates([x, y]: Coordinates): Partial<PriceData> {
    if (isNil(x) || isNil(y)) {
      return {}
    } else {
      return baiduTable.value[x][y]
    }
  }

  return {
    baiduCoordinates,
    qihu360Coordinates,
    baiduPrice,
    qihu360Price,
    baiduTable,
    qihu360Table,
    currentBaiduPriceData,
    current360PriceData,
    currentBaiduPrice,
    current360Price,
    current360OriginPrice,
    creativePrice,
    creativeOriginPrice
  }
}
